package com.x.provider.customer.service.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.x.provider.customer.mapper.EntityAddressMapper;
import com.x.provider.api.customer.model.dto.common.SetEntityAddressReqDTO;
import com.x.provider.customer.model.domain.common.EntityAddress;
import com.x.provider.customer.model.query.common.EntityAddressQuery;
import com.x.provider.customer.service.directory.RegionService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class EntityAddressServiceImpl implements EntityAddressService {

    private final EntityAddressMapper entityAddressMapper;
    private final RegionService regionService;

    public EntityAddressServiceImpl(EntityAddressMapper customerAddressMapper,
                                    RegionService regionService){
        this.entityAddressMapper = customerAddressMapper;
        this.regionService = regionService;
    }

    @Override
    public List<EntityAddress> listEntityAddress(Long customerId) {
        return list(EntityAddressQuery.builder().entityId(customerId).build());
    }

    @Override
    public List<EntityAddress> listEntityAddress(List<Long> customerIdList) {
        return list(EntityAddressQuery.builder().entityIdList(customerIdList).build());
    }

    @Override
    public void setEntityAddress(SetEntityAddressReqDTO setCustomerAddressRequestBO) {
        if (setCustomerAddressRequestBO.getId() != null){
            final EntityAddress customerAddress = get(EntityAddressQuery.builder().id(setCustomerAddressRequestBO.getId()).build());
            prepare(setCustomerAddressRequestBO, customerAddress);
            entityAddressMapper.updateById(customerAddress);
        }
        else {
            EntityAddress customerAddress = EntityAddress.builder().entityId(setCustomerAddressRequestBO.getEntityId())
                    .addressType(setCustomerAddressRequestBO.getAddressType()).build();
            prepare(setCustomerAddressRequestBO, customerAddress);
            entityAddressMapper.insert(customerAddress);
        }
    }

    private void prepare(SetEntityAddressReqDTO src, EntityAddress dest) {
        dest.setRegionId(src.getRegionId());
        dest.setRegionLevel(src.getRegionLevel());
    }

    public EntityAddress get(EntityAddressQuery query){
        return entityAddressMapper.selectOne(buildQuery(query));
    }

    public List<EntityAddress> list(EntityAddressQuery query){
        return entityAddressMapper.selectList(buildQuery(query));
    }

    public LambdaQueryWrapper<EntityAddress> buildQuery(EntityAddressQuery customerAddressQuery){
        LambdaQueryWrapper<EntityAddress> query = new LambdaQueryWrapper<>();
        if (customerAddressQuery.getId() != null){
            query = query.eq(EntityAddress::getId, customerAddressQuery.getId());
        }
        if (!CollectionUtils.isEmpty(customerAddressQuery.getIdList())){
            query = query.in(EntityAddress::getId, customerAddressQuery.getIdList());
        }
        if (customerAddressQuery.getEntityId() != null){
            query = query.eq(EntityAddress::getEntityId, customerAddressQuery.getEntityId());
        }
        if (customerAddressQuery.getAddressType() != null){
            query = query.eq(EntityAddress::getAddressType, customerAddressQuery.getAddressType());
        }
        if (!CollectionUtils.isEmpty(customerAddressQuery.getEntityIdList())){
            query = query.in(EntityAddress::getEntityId, customerAddressQuery.getEntityIdList());
        }
        return query;
    }
}
